package com.zzyl.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> selectQuery(String name, Integer status, Integer pageNum, Integer pageSize) {
        //分页
        PageHelper.startPage(pageNum, pageSize);
        //调用Mapper层查询
        Page<NursingProjectVo> nursingProjects = nursingProjectMapper.selectQuery(name, status);
        return PageResponse.of(nursingProjects, NursingProjectVo.class);
    }

    @Override
    public void addNursingProject(NursingProjectDto dto) {
        //dto转entity'
        NursingProject entity = BeanUtil.toBean(dto, NursingProject.class);
        //查询name 值是否存在
        Integer count = nursingProjectMapper.selectNursingProjectName(entity.getName());
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_NAME);
        }
        //调用Mapper层
        nursingProjectMapper.insertNursingProject(entity);
    }

    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectNursingProjectById(id);
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }

    @Override
    public void updateNursingProject(NursingProjectDto dto) {
        //dto转entity'
        NursingProject entity = BeanUtil.toBean(dto, NursingProject.class);
        //查询name 值是否存在
        Integer count = nursingProjectMapper.selectNursingProject(entity.getName(), entity.getId());
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_NAME);
        }
        nursingProjectMapper.updateNursingProject(entity);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingProjectMapper.updateStatus(id, status);
    }

    @Override
    public void deleteById(Long id) {
        //如果护理项目是启用状态 也不能删除
        NursingProject nursingProject = nursingProjectMapper.selectNursingProjectById(id);
        if (nursingProject == null) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_NOT_EXIST);
        }
        if (nursingProject.getStatus() == 1) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_DELETE_FAIL);
        }
        nursingProjectMapper.deleteById(id);
    }




    @Override
    public List<NursingProjectVo> selectAllNursingProject() {

        List<NursingProjectVo> nursingProjectVos = nursingProjectMapper.selectAllNursingProject();
        return nursingProjectVos;
    }
}